package com.example.qt.ptplatform.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.qt.ptplatform.entity.Seed;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface SeedMapper extends BaseMapper<Seed> {
    //查询活跃的种子
    @Select("SELECT * FROM seeds WHERE end_time IS NULL")
    List<Seed> getAllActiveSeed();
    // 更新做种人数为0的种子的结束时间
    @Update("<script>" +
            "UPDATE seeds SET end_time = #{endTime} " +
            "WHERE torrent_id IN (" +
            "   SELECT torrent_id FROM torrents WHERE info_hash IN " +
            "   <foreach item='infoHash' collection='infoHashes' open='(' separator=',' close=')'>" +
            "       #{infoHash}" +
            "   </foreach>" +
            ") AND end_time IS NULL" +
            "</script>")
    void updateEndTimeForZeroSeeders(@Param("infoHashes") List<String> infoHashes,
                                     @Param("endTime") LocalDateTime endTime);
}
